﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace btnet.pages
{
    public partial class hg_log : CommonPage
    {
        protected string log_result;
        protected string repo;
        protected string file_path;
        protected string string_affected_path_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);

            security = new Security();
            security.check_security(HttpContext.Current, Security.ANY_USER_OK);


            // get info about revision

            string sql = @"
select hgrev_repository, hgrev_revision, hgap_path, hgrev_bug
from hg_revisions
inner join hg_affected_paths on hgap_hgrev_id = hgrev_id
where hgap_id = $id
order by hgrev_revision desc, hgap_path";

            int hgap_id = Convert.ToInt32(Bug_Util.sanitize_integer(Request["id"]));

            string_affected_path_id = Convert.ToString(hgap_id);
            sql = sql.Replace("$id", string_affected_path_id);

            DataRow dr = DbUtil.get_datarow(sql);


            // check if user has permission for this bug
            int bugid = (int)dr["hgrev_bug"];

            int permission_level = Bug.get_bug_permission_level(bugid, security);
            if (permission_level == Security.PERMISSION_NONE)
            {
                Response.Write("You are not allowed to view this item");
                Response.End();
            }

            idlog.Text = string_affected_path_id;

            repo = (string)dr["hgrev_repository"];
            string revision = Convert.ToString((int)dr["hgrev_revision"]);
            file_path = (string)dr["hgap_path"];

            log_result = VersionControl.hg_log(repo, revision, file_path);

        }

        ///////////////////////////////////////////////////////////////////////
        protected void fetch_and_write_history()
        {


            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<log>" + log_result + "</log>");

            XmlNodeList revisions = doc.GetElementsByTagName("changeset");

            int row = 0;

            // read backwards
            for (int i = revisions.Count - 1; i > -1; i--)
            {
                XmlElement changeset = (XmlElement)revisions[i];

                string revision = changeset.GetAttribute("node");
                string author = changeset.GetElementsByTagName("auth")[0].InnerText;
                string date = changeset.GetElementsByTagName("date")[0].InnerText;
                string desc = changeset.GetElementsByTagName("desc")[0].InnerText;
                string path = changeset.GetElementsByTagName("file")[0].InnerText;

                Response.Write("<tr><td class=datad>" + revision);
                Response.Write("<td class=datad>" + author);
                Response.Write("<td class=datad>" + date);
                Response.Write("<td class=datad>" + path);
                //        Response.Write("<td class=datad>" + action);
                //        Response.Write("<td class=datad>" + copy_from);
                //        Response.Write("<td class=datad>" + copy_from_rev);

                Response.Write("<td class=datad>" + desc.Replace(Environment.NewLine, "<br/>"));


                Response.Write("<td class=datad><a target=_blank href=hg_view.aspx?id=" + string_affected_path_id
                    + "&rev=" + revision
                    + ">");

                Response.Write("view</a>");

                Response.Write("<td class=datad><a target=_blank href=hg_blame.aspx?id=" + string_affected_path_id
                    + "&rev=" + revision
                    + ">");

                Response.Write("annotated</a>");

                Response.Write("<td class=datad><a id=" + revision
                    + " href='javascript:sel_for_diff("
                    + Convert.ToString(++row)
                    + ",\""
                    + revision
                    + "\")'>select for diff</a>");


            }

        }
    }
}
